package com.atguigu.gmall.web.controller;

import com.atguigu.gmall.item.client.ItemClientFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.LinkedHashMap;
import java.util.Map;

@Controller
public class WebController {

    @Autowired
    private ItemClientFeign itemClientFeign;

    @GetMapping("/{skuId}.html")
    public String itemPage(@PathVariable("skuId") Long skuId, Model model) {

        Map<String, Object> resultMap = itemClientFeign.getItem(skuId);
        LinkedHashMap<String, String> skuInfo = (LinkedHashMap<String, String>) resultMap.get("skuInfo");
        if (null == skuInfo || null == skuInfo.get("id")) {
            return "redirect:item/index";
        }
        model.addAllAttributes(resultMap);
        return "item/index";
    }
}
